
import sys
import os
import re


def get_uuid_by_cmd_from_file(filepath,outputfile='',outputfilemode='w'):
    fp = open(filepath,"r+")
    buf=fp.readlines()
    fp.close()
    '''转换成大写'''
    bufupper = [i.upper()  for i in  buf]
    ''' 去掉空格换行括号0X'''
    buf_list_upper_no_space_enter = [x.replace('\t','').replace('\n','').replace(' ','').\
    replace('\r','').replace('{','').replace('}','').replace('0X','').replace(',','') for x in bufupper]

    '''把BUF转换成一行一行，分别处理每一行'''
    count = 0
    output_buf=''
    buf_list_del_repeat = list(set(buf_list_upper_no_space_enter))
    for i in buf_list_del_repeat:
        if(i.startswith('EA99162011')and(i.endswith('00'))and(len(i)==44)):
            count = count+1
            output_buf+=i[10:-2]+'\n'
        elif(len(i)==32):
            count = count+1
            #print('+++++'+i+'+++++')
            output_buf+=i+'\n'

            
    print(output_buf)


    '''输出到文件'''

    if(outputfile!=''):
        print(outputfile)
        fpo = open(outputfile,outputfilemode)
        fpo.write(output_buf)
        fpo.close()


    return count

def get_uuid_by_cmd_from_dir(filepath,outputfile='',outputfilemode='a'):
    if(os.path.isdir(filepath)):
        print("is dir")
    else:
        print("is not dir")
        return
    list_dirs = os.walk(filepath)
    for root, dirs, files in list_dirs: 
        for f in files:
            print(os.path.join(root, f))
            get_uuid_by_cmd_from_file(os.path.join(root, f),outputfile,outputfilemode)








'''exzample'''
#get_uuid_by_cmd_from_file('./datafile/UUid.txt','./123123','w')

if(os.path.isfile('1122')):
    os.remove('1122')
    print('remove old file')

get_uuid_by_cmd_from_dir('./datafile/','1122')
